<template>
	<z-paging ref="paging" v-model="dataList" @query="queryList"
		:loading-more-custom-style="{ paddingBottom: 'env(safe-area-inset-bottom)' }">
		<template #top>
			<view class="zpagin_top">
				<nav-bar :iconLeft="true" />
				<view class="search">
					<custom-search v-model="queryParams.search" @search="refreshList" placeholder="请输入生产单号/计划号/客户名称" />
				</view>
			</view>
		</template>

		<uni-card v-for="item in dataList" :key="item.id" margin="10px" spacing="0">
			<template v-slot:title>
				<uni-list>
					<uni-list-item :title="item.order_code" thumbSize="lg">
						<template v-slot:footer>
							<up-tag size="mini" :text="status_list[item.status_display].text" plain plainFill
								:type="status_list[item.status_display].type" />
						</template>
					</uni-list-item>
				</uni-list>
			</template>
			<view class="ctent">货品名称：{{ item.product_name }}</view>
			<view class="ctent">工序名称：{{ item.process_name }}</view>
			<view class="con">
				<view class="ctent">登记数量：{{ item.now_num }}</view>
				<view class="ctent">登记用户：{{ item.register_full_name }}</view>
			</view>
			<view class="con">
				<view class="ctent">不合格数：{{ item.no_ok_num }}</view>
				<view class="ctent">实制数量：{{ item.reality_num }}</view>
			</view>
			<view class="ctent">登记时间：{{ item.create_date }}</view>
			<view class="con">
				<view class="con_button" v-if="$hasPerm(['70601'])" @click.stop="register(item)" style="margin-left: auto;">
					质检/审核
				</view>
			</view>
		</uni-card>
		<template #bottom>
			<custom-popup :popupShow="show" mode="center" :title="'工序质检' + title" :closeOnClickOverlay="false"
				:closeable="false" width="90%">
				<view style="background-color: #fff; padding: 0 30rpx 20rpx;">
					<uni-forms labelPosition="left" :model="form" :rules="rules" ref="refForm" labelWidth="90px">
						<uni-forms-item label="登记数量" name="now_num">
							<uni-easyinput v-model="form.now_num" placeholder="请输入生产计划单号" class="iptBor" :inputBorder="false"
								:disabled="true" />
						</uni-forms-item>
						<uni-forms-item label="实制数量" name="reality_num">
							<uni-easyinput v-model="form.reality_num" placeholder="请输入生产计划单号" class="iptBor" :inputBorder="false"
								:disabled="true" />
						</uni-forms-item>
						<uni-forms-item label="不合格数量" name="no_ok_num" required>
							<u-number-box v-model="form.no_ok_num" min="0" :max="form.now_num"></u-number-box>
						</uni-forms-item>
						<uni-forms-item label="不合格描述" name="no_ok_desc">
							<uni-easyinput type="textarea" v-model="form.no_ok_desc" placeholder="请输入备注" />
						</uni-forms-item>
						<uni-forms-item label="状态" name="status">
							<up-radio-group v-model="form.status" placement="row">
								<up-radio class="radioa" label="待审核" name="1" />
								<up-radio class="radioa" label="通过(含部分合格)" name="2" />
								<up-radio class="radioa" label="返修" name="3" />
							</up-radio-group>
						</uni-forms-item>
					</uni-forms>
					<view class="btn-box">
						<u-button text="取 消" @click="show = false"></u-button>
						<u-button type="primary" text="提 交" @click="save"></u-button>
					</view>
				</view>
			</custom-popup>
		</template>
	</z-paging>
</template>
<script setup>
import { reactive, ref, } from 'vue';
import pCheckApi from '../../../api/production/p_check'

const status_list = {
	'待审核': { text: '待审核', type: 'warning' },
	'已审核': { text: '已审核', type: 'success' },
	'已完成': { text: '已完成', type: 'success' }
}


//数据请求
const paging = ref(null);
const dataList = ref([]);
const queryParams = reactive({
	search: '',
})
function queryList(pageNo, pageSize) {
	const params = {
		page: pageNo,
		page_size: pageSize,
		status: '1,3',
		...queryParams
	}
	pCheckApi.get_production_register(params).then((res) => {
		paging.value.complete(res.results);
	})
}

const show = ref(false)
const refForm = ref(null)
const form = reactive({
	id: '',
	now_num: '',
	reality_num: null,
	no_ok_num: '0',
	no_ok_desc: '',
	status: ''
})
const fields = Object.keys(form)
const rules = {
	no_ok_num: { rules: [{ required: true, message: '请输入不合格数量' }] },
}
function save() {
	refForm.value.validate().then(() => {
		const params = uni.$lodash.cloneDeep(form) // 深拷贝一份form表单
		pCheckApi.patch_production_register(params, params.id).then(res => {
			show.value = false
			refreshList()
		})

	}).catch((error) => {
		uni.$u.toast(error[0].errorMessage);
	})
}
const title = ref('工序登记')
function register(item) {
	title.value = item.process_name
	const record = uni.$lodash.pick(item, fields)
	record.status = '1'
	Object.assign(form, record)
	show.value = true
}
//编辑跳转刷新
function refreshList() {
	paging.value.reload();
}

</script>

<style lang="scss" scoped>
.radioa {
	margin-right: 30rpx;
}
</style>